import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/base/base_page.dart';
import 'package:provider/provider.dart';
import 'base_model.dart';

abstract class BaseStateFulWidget<T extends BaseModel>
    extends StatefulWidget with BasePage<T> {
  BaseStateFulWidget() {
    init();
  }

  @override
  State<StatefulWidget> createState() => _BaseStateFulWidget(createModel());

}

class _BaseStateFulWidget<T extends BaseModel>
    extends State<BaseStateFulWidget<T>> {

  final T model;

  _BaseStateFulWidget(this.model);


  @override
  void initState() {
    widget.beforeBuild(context);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<T>(
      create: (context) => model,
      child: Consumer<T>(builder: (context, data, child) {
        return widget.onBuild(data);
      }),
    );
  }
}
